﻿using System;
using System.IO;
using System.Reflection;
using Interfaz;
using System.Collections.Generic;

namespace CalculadoraSimple
{
    public class Handle
    {
        private List<IPlugin> ListaPlugins;

        public Handle()
        {
            ListaPlugins = new List<IPlugin>();
            this.CargarPlugins();
        }


        public void CargarPlugins()
        {
            ListaPlugins.Clear();

            String Path = AppDomain.CurrentDomain.BaseDirectory;

            foreach (string NombreArchivo in Directory.GetFiles(Path))
            {
                FileInfo file = new FileInfo(NombreArchivo);

                if (file.Extension.Equals(".dll"))
                {
                    Assembly pluginAssembly = Assembly.LoadFrom(NombreArchivo);

                    foreach (Type pluginType in pluginAssembly.GetTypes())
                    {
                        if (pluginType.IsPublic && !pluginType.IsAbstract)
                        {
                            Type typeInterface = pluginType.GetInterface("Interfaz.IPlugin", true);

                            if (typeInterface != null)
                            {
                                IPlugin MiPlugin = (IPlugin)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
                                ListaPlugins.Add((IPlugin)MiPlugin);
                            }
                        }
                    }
                }
            }
        }


        public List<IPlugin> getPlugins()
        {
            return ListaPlugins;
        }
    }
}
